package com.hakim.wfb.platform.controller;

import com.hakim.wfb.customer.params.team.*;
import com.hakim.wfb.customer.params.trans.ConsumeRecordParameter;
import com.hakim.wfb.customer.result.team.TeamInvestRecordView;
import com.hakim.wfb.customer.result.team.TeamLevelDetailView;
import com.hakim.wfb.customer.result.team.TeamMemberInfoView;
import com.hakim.wfb.customer.result.team.TeamOutlineView;
import com.hakim.wfb.customer.result.trans.CreditPointView;
import com.hakim.wfb.customer.service.TeamService;
import com.hakim.wfb.customer.service.UserService;
import com.hkfs.fundamental.api.data.ListResultEx;
import com.hkfs.fundamental.api.data.ObjectResultEx;
import com.hkfs.fundamental.api.data.ResultEx;
import com.hkfs.fundamental.api.params.SessionIdentity;
import com.hkfs.fundamental.session.SessionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by walkman on 2017/7/12.
 */
@RequestMapping(value = "/team", name = "团队")
@RestController
public class TeamController {

    @Autowired
    private UserService userService;

    @Autowired
    private TeamService teamService;

    /**
     * 获取团队会员列表
     *
     * @param parameter
     * @param request
     * @return
     */
    @RequestMapping(value = "/members", name = "获取团队会员列表", method = RequestMethod.POST)
    public ListResultEx<TeamMemberInfoView> getTeamMembers(GetTeamMemberParameter parameter, HttpServletRequest request) {

        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        parameter.setUserId(identity.getUuid());
        return teamService.getTeamMembers(parameter);
    }

    @RequestMapping(value = "/children", name = "获取直推会员列表", method = RequestMethod.POST)
    public ListResultEx<TeamMemberInfoView> getChildren(GetTeamMemberParameter parameter, HttpServletRequest request) {

        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        parameter.setUserId(identity.getUuid());
        return teamService.getChildren(parameter);
    }

    /**
     * 获取团队待激活会员列表
     *
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/inactive_members", name = "获取团队待激活会员列表", method = RequestMethod.POST)
    public ListResultEx<TeamMemberInfoView> getInactiveTeamMembers(GetTeamMemberParameter parameter, HttpServletRequest request) {
        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        parameter.setUserId(identity.getUuid());
        return teamService.getInactiveTeamMembers(parameter);
    }

    /**
     * 获取直推待激活会员列表
     *
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/inactiveChildren", name = "获取直推待激活会员列表", method = RequestMethod.POST)
    public ListResultEx<TeamMemberInfoView> getInactiveChildren(GetTeamMemberParameter parameter, HttpServletRequest request) {
        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        parameter.setUserId(identity.getUuid());
        return teamService.getInactiveChildren(parameter);
    }

    /**
     * 获取团队会员信息概要
     *
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/outline", name = "获取团队会员信息概要", method = RequestMethod.POST)
    public ObjectResultEx<TeamOutlineView> getTeamOutline(GetTeamOutlineParameter parameter, HttpServletRequest request) {
        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        parameter.setUserId(identity.getUuid());
        return teamService.getTeamOutline(parameter);
    }

    /**
     * 获取团队等级详情
     *
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/level", name = "获取团队等级详情", method = RequestMethod.POST)
    public ObjectResultEx<TeamLevelDetailView> getTeamLevelInfo(GetTeamLevelDetailParameter parameter, HttpServletRequest request) {
        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        parameter.setUserId(identity.getUuid());
        return teamService.getTeamLevelInfo(parameter);
    }

    /**
     * 获取团队会员投资记录列表
     *
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/invest_record", name = "获取团队等级详情", method = RequestMethod.POST)
    public ListResultEx<TeamInvestRecordView> getTeamInvestRecord(GetTeamInvestRecordParameter parameter, HttpServletRequest request) {
        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        parameter.setUserId(identity.getUuid());
        return teamService.getTeamInvestRecord(parameter);
    }

    /**
     * 激活团队成员
     *
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/active_member", name = "激活团队成员", method = RequestMethod.POST)
    public ResultEx activateMember(ActivateMemberParameter parameter, HttpServletRequest request) {
        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        parameter.setUserId(identity.getUuid());
        return userService.activateMember(parameter.getActivatedUserId(), parameter.getUserId());
    }

    @RequestMapping(value = "acheivement", name = "团队业绩", method = RequestMethod.POST)
    public ResultEx teamAcheivement(HttpServletRequest request) {
        SessionHandler sessionHandler = new SessionHandler();
        SessionIdentity sessionIdentity = sessionHandler.getSessionIdentity(request, SessionIdentity.class);
        String userid = sessionIdentity.getUuid();
        return teamService.teamAcheivement(userid);
    }

    @RequestMapping(value = "/getTeamAchieve", name = "获取团队业绩", method = RequestMethod.POST)
    public ListResultEx<CreditPointView> getTeamAchieve(HttpServletRequest request) {
        ListResultEx<CreditPointView> res = new ListResultEx<CreditPointView>();
        try {
            SessionHandler handler = new SessionHandler();
            SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
            ConsumeRecordParameter parameter = new ConsumeRecordParameter();
            parameter.setUserId(identity.getUuid());
            return res.makeSuccessResult();
        } catch (Exception e) {
            return res.makeFailedResult("查询失败");
        }
    }

}
